package com.itboye.sunsun.choosecity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.homesensen.alert.ByAlert;
import com.itboye.sunsun.adapter.NavigationAdapter;
import com.itboye.sunsun.beans.NavigationDetailBean;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
import com.itboye.sunsunhome.R;
import com.umeng.socialize.utils.Log;

import java.util.ArrayList;

public class Navigationactivity extends BaseActivity {

	ListView listView;
	NavigationAdapter adapter;
	private ArrayList<NavigationDetailBean> dataList = new ArrayList<NavigationDetailBean>();
	TextView add_shap_title_tv;// 标题
	ImageView close_icon;
	String cityCode = "";
	private String areaCode;
	private boolean searchType;
	private NavigationDetailBean navi;

	@Override
	@SuppressLint("InlinedApi")
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_navigation);
		setStatusBarColor(R.color.home_blue);
		cityCode = getIntent().getStringExtra("cityCode");
		areaCode = getIntent().getStringExtra("areaCode");
		navi=(NavigationDetailBean) getIntent().getSerializableExtra("navi");
		searchType = getIntent().getBooleanExtra("searchType", true);
		if(navi!=null){//选中了
			dataList.add(navi);
		}else{//没有选中
			getDetailNaigation(cityCode, areaCode);
		}
		
		
		add_shap_title_tv.setText("导航");
		close_icon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		adapter = new NavigationAdapter(dataList, this);
		listView.setAdapter(adapter);
		// initData();
	}

	private void getDetailNaigation(String cityCode, String areaCode) {
		System.out.println("relyoutDaoHang"+cityCode+areaCode);
		// TODO Auto-generated method stub
		MyJsonRequest.Builder<ArrayList<NavigationDetailBean>> builder = new MyJsonRequest.Builder<ArrayList<NavigationDetailBean>>();
		if (searchType) {// 为true是通过城市编码搜索
			builder.param("city", cityCode);
		} else {// 为false为通过区域搜索
			builder.param("area", areaCode);
		}
		builder.apiVer("100")
				.typeKey("BY_BranchOffice_search")
				.param("city", cityCode)
				.param("area",areaCode)
				.requestListener(
						new XRequestListener<ArrayList<NavigationDetailBean>>() {

							@Override
							public void onResponse(
									ArrayList<NavigationDetailBean> orderBean) {
								Log.v("count", orderBean.size() + "");
								if (orderBean != null)
									dataList.addAll(orderBean);
								adapter.notifyDataSetChanged();

							}
						}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						// onPullDownError(adapter, xlistview, exception, code,
						// msg);
						ByAlert.alert("网络错误！");
					}
				});

		MyJsonRequest<ArrayList<NavigationDetailBean>> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
}
